package Controler;

import java.util.ArrayList;

import Client.BusinessImplements;
/**
 * @author Lucie
 *class Update_Author_Controler
 *Class variables:msg,flag
 *Class methods:set_new_auther,get_all_authors,check_input,get_msg,set_msg
 */
public class Update_Author_Controler {
	
	private String msg; 
	private boolean flag;
	
	/**
	 *  Constructor initializes the class variables
	 */
	public Update_Author_Controler ()
	{
		flag=true;
		msg=new String();
		msg=null;
	}
	/**
	 * 
	 * @return String white all of the problems that occurred in the class
	 */
	public String get_msg()
	{
		return msg;
	}
	/**
	 *  @param new_msg
	 * This method chain messages into a string ,
	 *  the purpose ultimately is to display on the screen all the failures
	 */
	public void set_msg(String new_msg)
	{
		if (msg==null)
			msg=new_msg;
		else
		msg+=new_msg;
		
	}
	
/**
 * 
 * @param author_id
 * @param new_authorname
 	* this  method call the class ;serverproxy, to Contact with the server,
	 * first we check the Parameters we get (check_input) if something is wrong we return false 
	 * then we check if the author we trying to update his details exist, by calling the method "get_Authorname" ,if he exist
	 * we check if the new author name is not exist (with method "check_author_exist") if all the checks are
	 * ok we call the method "change_author" that update in the data base the name of the author that has id was given
	 *  @return  boolean:If the calls in the method failed it returns false otherwise returns true
 */
	 public boolean set_new_auther(String author_id,String new_authorname)
		{
		 
		 ArrayList<String> str_check= new  ArrayList<String>();
		 ArrayList<String> str_author=new  ArrayList<String>();
		
		 check_input( author_id,new_authorname);
		 	if (flag==false)
		 return false;
	 
		 	str_author=BusinessImplements.GetInstance().getServerProxy().get_Authorname( author_id);
	 
		 	if (str_author.get(0).equals("false"))
		 	{
		 		set_msg("\nauthor not exist in the system"); 
		 		return false;
		 	}
		 	
		 	str_check=BusinessImplements.GetInstance().getServerProxy().check_author_exist(new_authorname);
		 	if (str_check.get(0).equals("true"))
		 	{
		 		set_msg("\nauthor allredy exist");
		 		return false;
		 	}
		 	str_check=BusinessImplements.GetInstance().getServerProxy().change_author(new_authorname, author_id);
		 	if (str_check.get(0).equals("false"))
		 	{
		 		set_msg("\nupdate faild"); 
		 		return false;
		 	}
		 	return flag;
}
	/**
	 * 
	 * @param author_id
	 * @param name
	 * @param new_msg
	  *This method chain messages into a string ,
	 * the purpose ultimately is to display on the screen all the failures
	 *
	 */
		public void check_input(String author_id,String name)
		{
			//
			if(author_id.equals(""))
			{
				set_msg("\nmising author id ");
				flag=false;
			}

			if(name.equals(""))
			{
				set_msg("\nmising author name ");
				flag=false;
			}
		
			
		}
	/**
 * this  method call the class ;serverproxy, to Contact with the server,
 * we call the method getAuthors() that return from the db all the authors that exist in the db
 * @return ArrayList<String> with all the authors names and id
	 */
	
	public   ArrayList<String> get_all_authors()
	{
		 ArrayList<String> str_authors=new  ArrayList<String>(); 

		str_authors=BusinessImplements.GetInstance().getServerProxy().getAuthors();
		
		
		return str_authors;
		
		
	}
	
	
	
}
